//
//  BookPlaceDetailSectionCell.m
//  FreeTrip
//
//  Created by 352324497@qq.com on 15/11/26.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "BookPlaceDetailSectionCell.h"

@interface BookPlaceDetailSectionCell ()
{
    CGFloat x;
}

@end

@implementation BookPlaceDetailSectionCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = NO;

        
        self.descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 * KScreenWidth / 375, 10 * KScreenWidth / 375, KScreenWidth - 20 * KScreenWidth / 375, 30 * KScreenWidth / 375)];
        self.descriptionLabel.font = [UIFont systemFontOfSize:18 * KScreenWidth / 375 weight:0.001];
        self.descriptionLabel.numberOfLines = 0;
        [self.contentView addSubview:_descriptionLabel];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumInteritemSpacing = 2;
        layout.minimumLineSpacing = 2;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.photosViewVc = [[UICollectionView alloc]initWithFrame:CGRectMake(10 * KScreenWidth / 375,  80 * KScreenWidth / 375, KScreenWidth - 20 * KScreenWidth / 375, 140 * KScreenWidth / 375) collectionViewLayout:layout];
        self.photosViewVc.showsHorizontalScrollIndicator = NO;
        
        self.photosViewVc.backgroundColor = [UIColor whiteColor];
        [self.photosViewVc registerClass:[BookPlaceDetailPhotosCell class] forCellWithReuseIdentifier:@"collectionCell"];
        [self.contentView addSubview:_photosViewVc];
        
    }
    return self;
}

-(void)setModel:(BookPlaceDetailTripModel *)model
{
    self.photoArray = [NSMutableArray array];
    _model = model;
    
    self.descriptionLabel.text = model.myDescription;
    self.photoArray = model.notesArray;
    CGRect bounds = [model.myDescription boundingRectWithSize:CGSizeMake(KScreenWidth - 20 * KScreenWidth / 375, 1000 * KScreenWidth / 375) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18 * KScreenWidth / 375] forKey:NSFontAttributeName] context:nil];
    self.descriptionLabel.frame = CGRectMake(10 * KScreenWidth / 375, 20 * KScreenWidth / 375, KScreenWidth - 20 * KScreenWidth / 375, bounds.size.height);
    if (model.notesArray.count > 0)
    {
        self.photosViewVc.frame = CGRectMake(10 * KScreenWidth / 375, bounds.size.height + 40 * KScreenWidth / 375, KScreenWidth - 20 * KScreenWidth / 375, 120 * KScreenWidth / 375);
        self.photosViewVc.dataSource = self;
        self.photosViewVc.delegate = self;
        [self.photosViewVc reloadData];
        return;
    }
    else
    {
        self.photosViewVc.frame = CGRectMake(10 * KScreenWidth / 375, bounds.size.height + 60 * KScreenWidth / 375, KScreenWidth - 20 * KScreenWidth / 375, 0);
    }
    
}

+(CGFloat)heightForCell:(BookPlaceDetailTripModel *)model
{
    CGRect bounds = [model.myDescription boundingRectWithSize:CGSizeMake(KScreenWidth - 20 * KScreenWidth / 375, 1000 * KScreenWidth / 375) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18 * KScreenWidth / 375] forKey:NSFontAttributeName] context:nil];
    
    if (model.notesArray.count > 0)
    {
        return 40 * KScreenWidth / 375 + bounds.size.height + 140 * KScreenWidth / 375;
    }
    else
    {
        return 40 * KScreenWidth / 375 + bounds.size.height;
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photoArray.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotosModel *model = self.photoArray[indexPath.item];
    CGFloat width = model.image_width * (120 * KScreenWidth / 375) / model.image_height;
    return CGSizeMake(width, 120 * KScreenWidth / 375);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BookPlaceDetailPhotosCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    PhotosModel *model = self.photoArray[indexPath.item];
    cell.model = model;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotosModel *model = self.photoArray[indexPath.item];
    NSInteger a = 0;
    for (NSInteger i = 0; i < self.photoArray.count; i++)
    {
        if ([model isEqual:self.photoArray[i]])
        {
            a = i;
        }
    }
    if ([self.delegate respondsToSelector:@selector(JumpPageWithArray:item:)])
    {
        [self.delegate JumpPageWithArray:self.photoArray item:a];
    }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
